import G6 from '@antv/g6';

function initNodeListeners(graph) {
    graph.on('node:mouseenter', (evt) => {
        const node = evt.item;
        const nodeModel = node.getModel();
        // 激活该节点的 hover 状态
        node.setState('hover', true);

        // 查找当前所有source id相同的edge，并激活
        const edges = graph.findAll('edge', (edge) => {
            const edgeModel = edge.getModel();
            return edgeModel.source === nodeModel.id + '';
        });

        for(const edge of edges) {
            edge.setState('hover', true);
        }
        const mode = graph.getCurrentMode();
        const supportEditModes = [];
        // console.log(mode)

        //如果当前是编辑状态，显示操作栏
        if(supportEditModes.includes(mode)) {
            node.setState('showActions', true);
        } else {
            node.setState('showActions', false);
        }
    });

    // 监听鼠标离开节点事件
    graph.on('node:mouseleave', (evt) => {
        const node = evt.item;
        const nodeModel = node.getModel();
        // 关闭该节点的 hover 状态
        node.setState('hover', false);

        // 查找当前所有source id相同的edge，并激活
        const edges = graph.findAll('edge', (edge) => {
            const edgeModel = edge.getModel();
            return edgeModel.source === nodeModel.id;
        });

        for(const edge of edges) {
            edge.setState('hover', false);
        }

        node.setState('showActions', false);
    });
}

function initEdgeListeners(graph) {
    // edges
    graph.on('edge:mouseenter', (ev) => {
        const edge = ev.item;
        const model = ev.item.getModel();
        if(!model.addingEdge) {
            graph.setItemState(edge, 'hover', true);
        }
    });

    graph.on('edge:mouseleave', (ev) => {
        const edge = ev.item;
        graph.setItemState(edge, 'hover', false);
    });


    graph.on('edgeAdded', (e) => {
        // 清除正在添加的状态
        graph.addingEdge = null;
        graph.workEdge = null;
        const edges = graph.save().edges;
        G6.Util.processParallelEdges(edges, 15, 'ref');
        // console.log(graph.getEdges())
        graph.getEdges().forEach((edge, i) => {
            graph.updateItem(edge, {
                curveOffset: edges[i].curveOffset,
                curvePosition: edges[i].curvePosition,
            });
        });
    });
}

function initBehaviorListeners(graph) {

}

/**
 * 单独节点位置排列
 * @param graph
 */
function initSingleNodeListeners(graph) {
    graph.on('afterlayout', () => {
        const nodes = graph.findAll('node', (node) => {
            const model = node.getModel();
            return model.comboName === 'SINGLE';
        })
        // console.log(nodes);
        for(const [index, node] of nodes.entries()) {
            graph.updateItem(node, {
                x: -100,
                y: 100 + index * 80,
            });
        }
    });
}

/**
 * 绘制集群内节点的path
 * @param graph
 */
function initClusterDrawListeners(graph) {
    graph.on('cluster-node', (shapes) => {
        setTimeout(() => {
            const edges = graph.getEdges();
            for(const shape of shapes) {
                let positiveEdges = edges.filter(edge => {
                    const model = edge.getModel();
                    return model.source === shape.id
                });
                let negativeEdges = edges.filter(edge =>{
                    const model = edge.getModel();
                    return model.target === shape.id;
                });
                console.log(positiveEdges, negativeEdges)
            }

        }, 0);
    });
}

export function registerEvents(graph: any) {
    initNodeListeners(graph);
    initEdgeListeners(graph);
    initBehaviorListeners(graph);
    initSingleNodeListeners(graph);
    initClusterDrawListeners(graph);
}
